﻿using HML.Design;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;

namespace HML
{
    /// <summary>
    /// TrackBarPlus渐变颜色集合
    /// </summary>
    [Editor(typeof(CollectionHelpEditor), typeof(UITypeEditor))]
    public class TrackBarPlusGradualColorCollection : IList, ICollection, IEnumerable
    {
        #region

        private ArrayList trackBarPlusGradualColorList = new ArrayList();

        private TrackBarPlus owner;

        #endregion

        internal TrackBarPlusGradualColorCollection(TrackBarPlus owner)
        {
            this.owner = owner;
        }

        #region IEnumerable

        public IEnumerator GetEnumerator()
        {
            TrackBarPlusGradualColor[] listArray = new TrackBarPlusGradualColor[this.Count];
            for (int index = 0; index < listArray.Length; ++index)
            {
                listArray[index] = (TrackBarPlusGradualColor)this.trackBarPlusGradualColorList[index];
            }
            return listArray.GetEnumerator();
        }

        #endregion

        #region ICollection

        public void CopyTo(Array array, int index)
        {
            for (int i = 0; i < this.Count; i++)
            {
                array.SetValue(this.trackBarPlusGradualColorList[i], i + index);
            }
        }

        public int Count
        {
            get { return this.trackBarPlusGradualColorList.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return (object)this; }
        }

        #endregion

        #region IList

        public int Add(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is TrackBarPlusGradualColor))
            {
                throw new ArgumentException("TrackBarPlusGradualColor");
            }

            return this.Add((TrackBarPlusGradualColor)value);
        }

        public int Add(TrackBarPlusGradualColor value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            this.trackBarPlusGradualColorList.Add(value);
            this.owner.Invalidate();
            return this.Count - 1;
        }

        public void Clear()
        {
            this.trackBarPlusGradualColorList.Clear();
            this.owner.Invalidate();
        }

        public bool Contains(object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            return this.IndexOf(value) != -1;
        }

        bool IList.Contains(object value)
        {
            if (value is TrackBarPlusGradualColor)
            {
                return this.Contains((TrackBarPlusGradualColor)value);
            }
            return false;
        }

        public int IndexOf(object value)
        {
            if (value is TrackBarPlusGradualColor)
            {
                return this.trackBarPlusGradualColorList.IndexOf(value);
            }
            return -1;
        }

        public void Insert(int index, object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is TrackBarPlusGradualColor))
            {
                throw new ArgumentException("TrackBarPlusGradualColor");
            }

            if (index > this.Count)
            {
                index = this.Count;
            }
            ArrayList tmp = new ArrayList();
            for (int i = 0; i < index; i++)
            {
                tmp.Add(this.trackBarPlusGradualColorList[i]);
            }
            tmp.Add(value);
            for (int i = index; i < this.Count; i++)
            {
                tmp.Add(this.trackBarPlusGradualColorList[i]);
            }
            this.trackBarPlusGradualColorList = tmp;
            this.owner.Invalidate();
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public void Remove(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is TrackBarPlusGradualColor))
            {
                throw new ArgumentException("TrackBarPlusGradualColor");
            }

            this.Remove((TrackBarPlusGradualColor)value);
        }

        public void Remove(TrackBarPlusGradualColor value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            this.trackBarPlusGradualColorList.Remove(value);
            this.owner.Invalidate();
        }

        public void RemoveAt(int index)
        {
            this.trackBarPlusGradualColorList.RemoveAt(index);
            this.owner.Invalidate();
        }

        public TrackBarPlusGradualColor this[int index]
        {
            get { return (TrackBarPlusGradualColor)this.trackBarPlusGradualColorList[index]; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                this.trackBarPlusGradualColorList[index] = (TrackBarPlusGradualColor)value;
                this.owner.Invalidate();
            }
        }

        object IList.this[int index]
        {
            get { return (object)this.trackBarPlusGradualColorList[index]; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (!(value is TrackBarPlusGradualColor))
                {
                    throw new ArgumentException("TrackBarPlusGradualColor");
                }

                this.trackBarPlusGradualColorList[index] = (TrackBarPlusGradualColor)value;
                this.owner.Invalidate();
            }
        }

        #endregion
    }

}
